package com.bls.cate.test2.base;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

public class BaseModelImpl implements BaseModel {


    @Override
    public void requestGetNetData(final int requestId, String url, CacheMode cacheMode, final BaseView baseView, HttpParams... httpParamsArray) {
        String cacheKey;
        HttpParams httpParams = new HttpParams();
        cacheKey = url.concat(httpParams.toString());

        if (httpParamsArray != null)
            for (HttpParams h : httpParamsArray) {
                httpParams.put(h);
            }

        OkGo.<String>get(url).tag(url).cacheKey(cacheKey).cacheMode(cacheMode).params(httpParamsArray == null ? null : httpParams).execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                baseView.onRequestNetResult(requestId, BaseView.RequestNetState.onStart, null, null);
            }

            @Override
            public void onSuccess(Response<String> response) {
                baseView.onRequestNetResult(requestId, BaseView.RequestNetState.onSuccess, response.body(), response);
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
                onSuccess(response);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                baseView.onRequestNetResult(requestId, BaseView.RequestNetState.onError, response.body(), response);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                baseView.onRequestNetResult(requestId, BaseView.RequestNetState.onFinish, null, null);
            }
        });
    }
}
